package cn.itsource.pethome.base.service.impl;

import cn.itsource.pethome.base.domain.dto.VerifyDto;
import cn.itsource.pethome.base.service.IImageVerifyCodeService;
import cn.itsource.pethome.constants.BaseConstants;
import cn.itsource.pethome.user.domain.User;
import cn.itsource.pethome.user.mapper.UserMapper;
import cn.itsource.pethome.util.BusinessException;
import cn.itsource.pethome.util.SendMsgUtil;
import cn.itsource.pethome.util.StrUtils;
import cn.itsource.pethome.util.VerifyCodeUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
@Service
public class ImageVerifyCodeServiceImpl implements IImageVerifyCodeService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;

    /**
     * 图形验证码
     * @param key
     * @return
     */
    @Override
    public String  imageVerifyCode(String key) {
        try {
            //1.判断key不为空
            if(key==null){
                throw new BusinessException("非法获取！！！");
            }
            //2.获取验证码 code
            String code = StrUtils.getComplexRandomString(4);
            //3.获取验证图片并放入code
            String verifyCode = VerifyCodeUtils.VerifyCode(110, 40, code);
            //将code存入redis中并设置失效时间
            redisTemplate.opsForValue().set(key+ BaseConstants.register.REGISTER,code,5, TimeUnit.MINUTES);
            return verifyCode;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 短信验证码
     * @param verifyDto
     */
    @Override
    public void sendSmsCode(VerifyDto verifyDto) {
        //1.判断手机号是否为空
        if(verifyDto.getPhone()==null){
            throw new BusinessException("手机号不能为空");
        }
        //2.判断判断图新验证码是否为空
        if(verifyDto.getImageCode()==null){
            throw new BusinessException("图新验证码不能为空");
        }
        //3.判断key是否为空
        if(verifyDto.getImageCodeKey()==null){
            throw new BusinessException("非法访问");
        }
        //4.通过key 在redis中查询图形验证码的value验证
        Object values = redisTemplate.opsForValue().get(verifyDto.getImageCodeKey() + BaseConstants.register.REGISTER);
        if(values==null){
            throw new BusinessException("验证码已失效！！");
        }
        if(!values.toString().equalsIgnoreCase(verifyDto.getImageCode())){
            throw new BusinessException("验证码输入错误，请重新输入！");
        }
        //5.去数据库查询手机号是否存在
        User user = userMapper.QueryByPhone(verifyDto.getPhone());
        if(user != null){
            throw new BusinessException("手机号已经注册，请登录");
        }
        //6.1.2设置Key
        String code = verifyDto.getPhone()+BaseConstants.register.REGISTER;
        //6.1.3设置 value
        String randomcode = StrUtils.getRandomString(4);
        //6.redis的短信验证码是否存在
        Object oldcode = redisTemplate.opsForValue().get(code);
        //6.1不存在
        if(oldcode==null){
            //添加时间戳
            String telValue = randomcode+":"+System.currentTimeMillis();
            //6.1.1生成验证码(三分钟有效)
            redisTemplate.opsForValue().set(code,telValue,3,TimeUnit.MINUTES);
        }else {
            //6.2存在验证码
            //6.2.1 判断当前整个key是否超过1分钟
            String[] split = oldcode.toString().split(":");
            //6.2.1.2  不超过 抛出异常
            if((System.currentTimeMillis()-Long.parseLong(split[1])) <60 *1000){
                throw new BusinessException("请勿重复发送验证码");
            }
            //6.2.1.1  超过 重写生成发送短信验证码 更新时间戳(把之前的取出来)
            redisTemplate.opsForValue().set(code,oldcode.toString().split(":")[0]+":"+System.currentTimeMillis(),3,TimeUnit.MINUTES);
            randomcode=split[0];
        }
        //发送短信
        //SendMsgUtil.sendMsgUtil(verifyDto.getPhone(),randomcode);
        System.out.println(randomcode);
    }
}
